﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows;
using System.Xml.Serialization;
using System.ComponentModel;

namespace BDSim
{
    /// <summary>
    /// 空间段模型
    /// </summary>
    public class SpaceSegment : SegmentBase
    {
        #region 字段
        
        private SpaceSegmentDataProvider _DataProvider = new SpaceSegmentDataProvider();

        ObservableCollection<Constellation> _ConstellationList = new ObservableCollection<Constellation>();

        [XmlIgnore,Browsable(false)]
        public ObservableCollection<Satellite> AllSatellite
        {
            get
            {
                ObservableCollection<Satellite> allSatellite = new ObservableCollection<Satellite>();
                foreach (var con in ConstellationList)
                {
                    foreach (var sat in con.SatelliteCollection)
                    {
                        sat.Parent = con;
                        allSatellite.Add(sat);
                    }
                }
                return allSatellite;
            }
        } 

        #endregion

        #region 属性

        #region 类别一（把相同类别的属性放在一起）
        /// <summary>
        /// 空间段数据交互模型
        /// </summary>
        [XmlIgnore, Browsable(false)]
        public SpaceSegmentDataProvider DataProvider
        {
            get { return _DataProvider; }
            set { _DataProvider = value; }
        }
        
        [Browsable(false)]
        public ObservableCollection<Constellation> ConstellationList
        {
            get { return _ConstellationList; }
            set { _ConstellationList = value; NotifyPropertyChanged("ConstellationList"); }
        }

        #endregion

        #endregion

        #region 构造函数（包含调用方法）
        public SpaceSegment()
        {
            foreach (var item in ConstellationList)
            {
                item.Parent = this;
            }
            _DataProvider.Parent = this;
            _DataProvider.DefineCommand();
            if (MainDispatcher.MainWindowDispatcher == null)
            {
                Menu.ItemsSource = _DataProvider.MenuCommandList;
            }
            else
            {
                MainDispatcher.MainWindowDispatcher.Invoke(new Action(() =>
                {
                    Menu.ItemsSource = _DataProvider.MenuCommandList;
                }));
            }
        }
        #endregion

        #region 初始化
        public override void Init()
        {

            base.Init();
            foreach (var item in ConstellationList)
            {
                item.Init();
            }

            //int index = 1;

            //foreach (var item in ConstellationList)
            //{
            //    foreach (var item1 in item.SatelliteCollection)
            //    {
            //        item1.Id = index;
            //        index++;
            //    }
            //}
        }
        #endregion

        #region 单步运行
        public override void StepRun()
        {
            base.StepRun();
            foreach (var item in ConstellationList)
            {
                item.StepRun();
            }
        }
        #endregion

        #region 
        #endregion

        #region 功能实现二
        #endregion


        internal void AddConstellation(Constellation constellation)
        {
            constellation.Parent = this;
            ConstellationList.Add(constellation);

            foreach (var sat in constellation.SatelliteCollection)
            {
                sat.Orbit.Parent = sat;
                sat.Clock.Parent = sat;
            }
        }
    }
}
